package com.jd.org.controller;

import com.jd.aop.anno.LogAnno;
import com.jd.basic.annotation.PreAuthorize;
import com.jd.basic.jwt.UserInfo;
import com.jd.basic.util.AjaxResult;
import com.jd.basic.util.LoginContext;
import com.jd.basic.util.PageList;
import com.jd.org.domain.Department;
import com.jd.org.query.DepartmentQuery;
import com.jd.org.service.IDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/dept")
@Api(value = "部门的API", description = "部门相关的CRUD功能")
public class DepartmentController {
    private Long i = 24L;
    @Autowired
    private IDepartmentService departmentService;


    /**
     * 之前定义方法
     */
//    @RequestMapping(value = "/findById",method = RequestMethod.GET)
//    public Department findById(Long id){
//        return departmentService.findById(id);
//    }

    /**
     * Restful 风格 路径变量
     * http://localhost:80/dept/1
     * 根据id查询一个
     * dId
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询详情")
    public Department findById(@ApiParam(value = "部门id", required = true) @PathVariable("id") Long id) {
        return departmentService.findById(id);

    }



    @PutMapping()
    @LogAnno(des = "添加或者修改")
    public  AjaxResult addOrUpdate(@RequestBody Department department) {
        System.err.println("测试测试Controller" + department);
        if (null != department.getId()) {
            // 修改
            departmentService.update(department);
        } else {
            // 添加
            departmentService.add(department);
        }
        return AjaxResult.me().success();
    }

    /**
     * 删除
     */
    @DeleteMapping("/{id}")
    @PreAuthorize(name="部门删除" ,sn="department:delete")
    public AjaxResult delete(@PathVariable("id") Long id) {
        System.err.println("删除方法" + id);
        departmentService.delete(id);
        return AjaxResult.me().success();
    }

//    @PostMapping ("/delete")
//    public void delete(){
//        Long id = i++;
//        departmentService.delete(id);
//    }

    /**
     * 分页查询
     */
    @PostMapping()
    public PageList<Department> queryByPage(@RequestBody DepartmentQuery query) {
        System.err.println("查询=========="+ query);
        return departmentService.queryByPage(query);
    }

    /**
     * 查询所有
     *
     * @return
     */
    @GetMapping()
    public List<Department> findAll() {

        return departmentService.findAll();
    }


    /**
     * 批量删除
     *
     * @return
     */
    @PatchMapping
    @LogAnno(des = "批量删除")
    public AjaxResult batchDelete(@RequestBody List<Long> ids) {
        System.err.println("批量删除测试" + ids);
        departmentService.batchDelete(ids);
        return  AjaxResult.me().success();
    }

    /**
     * 无限级数
     */

    @GetMapping("/tree")
    public List<Department> tree(HttpServletRequest request){
        UserInfo userInfo = LoginContext.getUserInfo(request);
        System.err.println("?+++++++++++++++++++++++" + userInfo);
        return departmentService.tree();
    }



}
